package com.pentajock.docdb.gui;

import com.pentajock.docdb.Main;
import com.pentajock.docdb.images.DBImages;
import com.pentajock.docdb.utils.Utils;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class StatusBar extends JPanel {
	private JFrame window;
	private JLabel label;
	private BufferedImage img = DBImages.RESIZE_ICON.getImage();
	private int imgWidth = img.getWidth();
	private int imgHeight = img.getHeight();
	
	public StatusBar(JFrame parent) {
		super(new FlowLayout());
		this.window = parent;
		//label = new JLabel("Status Bar Ahoy!");
		label = new JLabel();
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		setPreferredSize(new Dimension(1, 23));
		
		((FlowLayout)getLayout()).setAlignment(FlowLayout.LEFT);
		add(label);
		
		/*
		 * This enforces the minimumSize set in the constructor but
		 * creates an unwanted behaviour with the resize graphic
		 * implemented above.
		 * Note: Java 1.6 fixes minimumSize.
		 *
		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				Dimension minSize = window.getMinimumSize();
				Dimension currentSize = window.getSize();
				if(currentSize.width < minSize.width) {
					currentSize.width = minSize.width;
				}
				if(currentSize.height < minSize.height) {
					currentSize.height = minSize.height;
				}
				window.setSize(currentSize);
			}
		});
		/**/
		
		MouseInputAdapter mia = new MouseInputAdapter() {
			private int imgWidth = DBImages.RESIZE_ICON.getImage().getWidth();
			private int imgHeight = DBImages.RESIZE_ICON.getImage().getHeight();
			
			private boolean cursorChanged = false;
			
			public void mouseMoved(MouseEvent e) {
				if(window.getExtendedState() != Frame.MAXIMIZED_BOTH) {
					Point p = e.getPoint();
					if(p.x > StatusBar.this.getWidth() - imgWidth - 2
					&& p.y > StatusBar.this.getHeight() - imgHeight - 2) {
						Main.setCursor(window, Cursor.SE_RESIZE_CURSOR);
						cursorChanged = true;
					}
					else if(cursorChanged) {
						Main.setCursor(window, Cursor.DEFAULT_CURSOR);
						cursorChanged = false;
					}
				}
			}
			
			public void mouseReleased(MouseEvent e) {
				if(cursorChanged) {
					Main.setCursor(window, Cursor.DEFAULT_CURSOR);
					cursorChanged = false;
				}
			}
			
			public void mouseDragged(MouseEvent e) {
				if(cursorChanged) {
					Rectangle rect = window.getBounds();
					Point mousePoint = MouseInfo.getPointerInfo().getLocation();
					
					window.setSize(new Dimension(mousePoint.x - rect.x, mousePoint.y - rect.y));
					window.validate(); // Needed. The window doesn't refresh its components without it.
				}
			}
		};
	
		addMouseListener(mia);
		addMouseMotionListener(mia);
	}
	
	public String getLabelText() {
		return label.getText();
	}
	
	public void setLabelText(String s) {
		label.setText(Utils.capitalize(s));
		//Main.printDebug(s);
	}
	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(window.getExtendedState() != Frame.MAXIMIZED_BOTH) {
			g.drawImage(img, this.getWidth() - imgWidth - 2, this.getHeight() - imgHeight - 2, null);
		}
	}
}
